package com.xcw;

import lombok.SneakyThrows;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author bhx
 * @Date 2022/7/8 2:12
 * @Description TODO
 */
@SuppressWarnings("all")
public class Test {

    public static void main(String[] args) {
        getVideo();
    }

    @SneakyThrows
    public static void getVideo() {
        String pageUrl = "https://haokan.baidu.com/v?pd=wisenatural&vid=8692295738536180695";
        URL url = new URL(pageUrl);
//        InputStream is = url.openStream();
//        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        String info = "";
        for (int i = 0; i < 3000; i++) { //我设置为网页的一共有3000 行 具体根据 你获取网页行数的大小来决定
            info = br.readLine();
            if (null != info) {
                String urlRegex = "src=\"(.*?\\.mp4)";//正则表达式
                Pattern pattern = Pattern.compile(urlRegex);
                Matcher matcher = pattern.matcher(info);
                if (matcher.find()) {//比对成功
                    for (int j = 0; j <= matcher.groupCount(); j++) {
                        String tmp = matcher.group(j);
                        if (!tmp.startsWith("src=")) {
                            System.out.println("ger mp4 src >" + tmp);
                        }
                    }
                }
            }
        }
    }
}
